package com.zc.manager.network.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.zc.component.network.core.security.Certificate;
import com.zc.component.network.core.security.CertificateManager;
import com.zc.component.network.core.security.DefaultCertificate;
import com.zc.manager.network.domain.IotCertificateInfo;
import com.zc.manager.network.enums.CertificateType;
import com.zc.manager.network.mapper.IotCertificateMapper;
import com.zc.manager.network.service.IIotCertificateService;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 证书Service业务层处理
 *
 * @author zhaoxianglong
 * @date 2022-11-7
 */
@Service
public class IotCertificateServiceImpl
        extends ServiceImpl<IotCertificateMapper, IotCertificateInfo>
        implements IIotCertificateService, CertificateManager {
    @Resource
    private IotCertificateMapper iotCertificateMapper;

    /**
     * 查询
     */
    @Override
    public List<IotCertificateInfo> listCertificate(IotCertificateInfo iotCertificateInfo) {
        LambdaQueryWrapper<IotCertificateInfo> queryChainWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(iotCertificateInfo.getCertificateName())) {
            queryChainWrapper.like(IotCertificateInfo::getCertificateName, iotCertificateInfo.getCertificateName());
        }
        if (!StringUtils.isEmpty(iotCertificateInfo.getCertificateType())) {
            queryChainWrapper.eq(IotCertificateInfo::getCertificateType, iotCertificateInfo.getCertificateType());
        }
        return list(queryChainWrapper);
    }

    /**
     * 新增
     */
    @Override
    public boolean addCertificate(IotCertificateInfo iotCertificateInfo) {
        iotCertificateInfo.setCreateTime(new Date());
        iotCertificateInfo.setUpdateTime(new Date());
        return save(iotCertificateInfo);
    }

    /**
     * 修改
     */
    @Override
    public boolean editCertificate(IotCertificateInfo iotCertificateInfo) {
        iotCertificateInfo.setUpdateTime(new Date());
        return updateById(iotCertificateInfo);
    }

    /**
     * 查询详情
     */
    @Override
    public IotCertificateInfo getCertificate(Long id) {
        return getById(id);
    }

    /**
     * 导出
     */
    @Override
    public List<IotCertificateInfo> selectIotCertificateList(IotCertificateInfo iotCertificateInfo) {
        return iotCertificateMapper.selectIotCertificateList(iotCertificateInfo);
    }

    @Override
    public Mono<Certificate> getCertificate(String id) {
        return Mono.just(getCertificate(Long.parseLong(id)))
                .map(entity -> {
            DefaultCertificate defaultCertificate = new DefaultCertificate(String.valueOf(entity.getId()), entity.getCertificateName());
            return CertificateType.getCertificateType(entity.getCertificateType()).init(defaultCertificate, entity.getCertificateConfig());
        });
    }
}
